package com.cn.jinl.controller;


import com.cn.jinl.api.IPlanManageService;
import com.cn.jinl.domain.Agv;
import com.cn.jinl.domain.GradeValue;
import com.cn.jinl.domain.PlanManage;
import com.cn.jinl.domain.PlanOrder;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/plan")
public class PlanManageController {

  @Autowired private IPlanManageService planManageService;

  @RequestMapping("/getPlanManageList/{frontToken}")
  @ResponseBody
  public JsonResponse<List<PlanManage>> getPlanManageList(
      @PathVariable(value = "frontToken", required = true) String frontToken,
      @RequestBody QueryForm queryForm)
      throws Exception {
    JsonResponse<List<PlanManage>> response = new JsonResponse<>();
    List<PlanManage> planManages = planManageService.getPlanManageList(queryForm);
    response.setResult(planManages);
    response.setSuccess(true);
    return response;
  }

  @RequestMapping("/getPlanManageMapList/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String, List<PlanManage>>> getPlanManageMapList(
      @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
    JsonResponse<Map<String, List<PlanManage>>> response = new JsonResponse<>();
    Map<String, List<PlanManage>> planManages = planManageService.getPlanManageMapList();
    response.setResult(planManages);
    response.setSuccess(true);
    return response;
  }

  @RequestMapping("/getPlanOrders/{frontToken}")
  @ResponseBody
  public JsonResponse<List<PlanOrder>> getPlanOrders(
      @PathVariable(value = "frontToken", required = true) String frontToken, String planId)
      throws Exception {
    JsonResponse<List<PlanOrder>> response = new JsonResponse<>();
    List<PlanOrder> planOrders = planManageService.getPlanOrders(planId);
    response.setResult(planOrders);
    response.setSuccess(true);
    return response;
  }

  @RequestMapping("/getPlanByOrderId/{frontToken}")
  @ResponseBody
  public JsonResponse<PlanManage> getPlanByOrderId(
      @PathVariable(value = "frontToken", required = true) String frontToken, String orderId)
      throws Exception {
    JsonResponse<PlanManage> response = new JsonResponse<>();
    PlanManage planManage = planManageService.getPlanByOrderId(orderId);
    response.setResult(planManage);
    response.setSuccess(true);
    return response;
  }

  @RequestMapping("/getUPlanManages/{frontToken}")
  @ResponseBody
  public JsonResponse<List<PlanManage>> getUPlanManages(
      QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken)
      throws Exception {
    JsonResponse<List<PlanManage>> response = new JsonResponse<>();
    List<PlanManage> planOrders = planManageService.getUPlanManages(queryForm);
    response.setResult(planOrders);
    response.setSuccess(true);
    return response;
  }

  @RequestMapping("/addPlanManage/{frontToken}")
  @ResponseBody
  public JsonResponse<String> addPlanManage(
      @RequestBody PlanManage planManage,
      @PathVariable(value = "frontToken", required = true) String frontToken)
      throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = planManageService.addPlanManage(planManage);
    return CommonUtil.getResponseData(response, result);
  }

  @RequestMapping("/updatePlanManage/{frontToken}")
  @ResponseBody
  public JsonResponse<String> updatePlanManage(
      @RequestBody PlanManage planManage,
      @PathVariable(value = "frontToken", required = true) String frontToken)
      throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = planManageService.updatePlanManage(planManage);
    return CommonUtil.getResponseData(response, result);
  }

  // 完成生产任务
  @RequestMapping("/updateCompletePlanManage/{frontToken}")
  @ResponseBody
  public JsonResponse<String> updateCompletePlanManage(
      String planId, @PathVariable(value = "frontToken", required = true) String frontToken)
      throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = planManageService.updateCompletePlanManage(planId);
    return CommonUtil.getResponseData(response, result);
  }

  // 成型启检任务选择
  @RequestMapping("/api/getSelectPlanManages")
  @ResponseBody
  public JsonResponse<List<PlanManage>> getSelectPlanManages(String type) throws Exception {
    JsonResponse<List<PlanManage>> response = new JsonResponse<>();
    List<PlanManage> planManages = planManageService.getSelectPlanManages(type);
    response.setResult(planManages);
    response.setSuccess(true);
    return response;
  }

  // agv生成新任务
  @RequestMapping("/callAgv/{frontToken}")
  @ResponseBody
  public JsonResponse<String> callAgv(
      String taskCode,String point, @PathVariable(value = "frontToken", required = true) String frontToken)
      throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = planManageService.callAgv(taskCode,point);
    return CommonUtil.getResponseData(response, result);
  }

    // agv继续任务
    @RequestMapping("/continueAgv/{frontToken}")
    @ResponseBody
    public JsonResponse<String> continueAgv(String taskCode, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<>();
        String result = planManageService.continueAgv(taskCode);
        return CommonUtil.getResponseData(response, result);
    }

  // agv回调判断走到了哪一个步骤
  @RequestMapping("/agvCallback")
  @ResponseBody
  public JsonResponse<String> agvCallback(String method,String taskCode) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = planManageService.agvCallback(method,taskCode);
    return CommonUtil.getResponseData(response, result);
  }

  //查询agv到了哪一个点位
  @RequestMapping("/getAgvPoint/{frontToken}")
  @ResponseBody
  public JsonResponse<Agv> getAgvPoint(
          @PathVariable(value = "frontToken", required = true) String frontToken)
          throws Exception {
    JsonResponse<Agv> response = new JsonResponse<>();
    Agv agv = planManageService.getAgvPoint();
    response.setResult(agv);
    response.setSuccess(true);
    return response;
  }

  // agv继续任务pda
  @RequestMapping("/api/continueAgvByApp")
  @ResponseBody
  public JsonResponse<String> continueAgvByApp(String taskCode) throws Exception {
    JsonResponse<String> response = new JsonResponse<>();
    String result = planManageService.continueAgv(taskCode);
    return CommonUtil.getResponseData(response, result);
  }

  //查询agv到了哪一个点位pda
  @RequestMapping("/api/getAgvPointByApp")
  @ResponseBody
  public JsonResponse<Agv> getAgvPointByApp() throws Exception {
    JsonResponse<Agv> response = new JsonResponse<>();
    Agv agv = planManageService.getAgvPoint();
    response.setResult(agv);
    response.setSuccess(true);
    return response;
  }

  //新增agv表数据
  @RequestMapping("/addAgv")
  @ResponseBody
  public JsonResponse<String>  addAgv(@RequestBody String jsonStr) throws Exception {
    JsonResponse<String>  response = new JsonResponse<>();
    String result  = planManageService.addAgv(jsonStr);
    return CommonUtil.getResponseData(response, result);
  }
  //修改agv表数据
  @RequestMapping("/updateAgv")
  @ResponseBody
  public JsonResponse<String>  updateAgv(@RequestBody String jsonStr) throws Exception {
    JsonResponse<String>  response = new JsonResponse<>();
    String result  = planManageService.updateAgv(jsonStr);
    return CommonUtil.getResponseData(response, result);
  }

  //计划分析
  @RequestMapping("/getPlanAnalysisList/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String, String>> getPlanAnalysisList(@RequestBody QueryForm queryForm, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception {
    JsonResponse<Map<String, String>>response = new JsonResponse<Map<String, String>>();
    Map<String, String>list  = planManageService.getPlanAnalysisList(queryForm);
    response.setResult(list);
    response.setSuccess(true);
    return  response;
  }

  //通过名称搜索规格
  @RequestMapping("/getGradeValueByName/{frontToken}")
  @ResponseBody
  public JsonResponse<List<GradeValue>> getGradeValueByName(
          @PathVariable(value = "frontToken", required = true) String frontToken,
          @RequestBody QueryForm queryForm)
          throws Exception {
    JsonResponse<List<GradeValue>> response = new JsonResponse<>();
    List<GradeValue> gradeValues = planManageService.getGradeValueByName(queryForm);
    response.setResult(gradeValues);
    response.setSuccess(true);
    return response;
  }
}
